package com.xuecheng.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 使用CORS解决跨域问题
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsFilter getCorsFilter(){

        CorsConfiguration configuration = new CorsConfiguration();
        //添加跨域的http方法
        configuration.addAllowedHeader("*");
        //添加运行哪些源路径进行跨域
        configuration.addAllowedOrigin("*");
        //添加运行哪些头信息可以被放行
        configuration.addAllowedHeader("*");
        //添加允许跨域发送cookie
        configuration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",configuration);
        return new CorsFilter(source);
    }
}
